// 数据字典
const express = require("express");
const apiRouter = express.Router();
const writeData = require("../../db/module/writeData");
const readData = require("../../db/module/readData");
const delData = require("../../db/module/delData");


// 添加白名单接口
apiRouter.post("/addWhiteList", async (req, res) => {
    try {
        const params = req.body;
        let result = await writeData(params, "whiteList");
        res.send(result);
    } catch (error) {
        res.send({
            code: 50000,
            msg: error,
        });
    }
});

// 查询白名单接口
apiRouter.post("/getWhiteList", async (req, res) => {
    try {
        const params = req.body;
        let result = await readData(params, "whiteList");
        res.send(result);
    } catch (error) {
        res.send({
            code: 50000,
            msg: error,
        });
    }
});

// 删除白名单接口
apiRouter.post("/delWhiteList", async (req, res) => {
    try {
        const params = req.body;
        let result = await delData(params, "whiteList");
        res.send(result);
    } catch (error) {
        res.send({
            code: 50000,
            msg: error,
        });
    }
});

// 同步接口白名单数据
apiRouter.post("/syncWhiteList", async (req, res) => {
    try {
        const endpoints = global.allApi

        // 查询当前数据库已有的 接口 
        let { data: existList, total } = await readData({ page: 1, size: 10000 }, "whiteList");

        // 计算出已经不存在的接口 然后将其删除
        if (total) {
            for (let i = 0; i < existList.length; i++) {
                const item = existList[i];
                let flag = false;   

                endpoints.forEach(endpoint => {
                    // 条件: 路径相等 && 方法相等 && 中间件相等
                    if (endpoint.path == item.InterfaceAddress && endpoint.methods.join(',') == item.methods && endpoint.middlewares.join(',') == item.middlewares) {
                        flag = true;
                    }
                });

                if (!flag) {
                    await delData({ id: item.id }, "whiteList");
                }
            }
        }

        // 将数据库中不存在的数据 添加到数据库中
        let newArray = []
        endpoints.forEach(item => {
            let flag = true;
            existList.forEach(row => {
                if (item.path == row.InterfaceAddress) {
                    flag = false;
                    row.methods = item.methods.join(",")
                    row.middlewares = item.middlewares.join(",")
                    newArray.push(row)
                }
            })

            if (flag) {
                newArray.push({
                    InterfaceAddress: item.path || '没有',
                    InterfaceName: '---',
                    isNext: false,
                    remark: '',
                    state: 1,
                    methods: item.methods.join(","),
                    middlewares: item.middlewares.join(",")
                })
            }
        })


        // 批量写入数据新
        let result = await writeData(newArray, "whiteList", true);
        res.send(result)
    } catch (error) {
        res.send({
            code: 50000,
            msg: error
        })
    }
});

module.exports = apiRouter;
